package server;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.net.InetAddress;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class ServerGUI extends JFrame
{
	DefaultListModel listModel;
	JTextArea log;
	
	public ServerGUI()
	{
		super("NFP Server"); //to set the frame (or window) title. super() calls the constructor of the inherited class
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //so the program exits when this window is closed
		setSize(480, 300); //set size of this frame
		
		Container pane = getContentPane(); //additional components must be added to this pane, not the frame
		pane.setLayout(new BorderLayout()); //layout managers are used to position components in panels
		
		listModel = new DefaultListModel(); //the data store for the list
		JList connectionList = new JList(listModel); //the GUI element
		//connectionList.setMaximumSize(new Dimension(120, 1000));
		//connectionList.setPreferredSize(new Dimension(120, 300)); //set the size of the list
		JScrollPane connectionScrollPane = new JScrollPane(connectionList); //so the list can scroll (most things don't get scrollbars automatically)
		//connectionScrollPane.setPreferredSize(new Dimension(120, 300)); //set the size of the list
		
		JPanel connectionPanel = new JPanel();
		connectionPanel.setLayout(new BoxLayout(connectionPanel, BoxLayout.Y_AXIS));
		connectionPanel.add(new JLabel("Peers"));
		connectionPanel.add(connectionScrollPane);
		//connectionPanel.setMaximumSize(new Dimension(120, 1000));
		connectionPanel.setPreferredSize(new Dimension(120, 300)); //set the size of the list
		
		pane.add(connectionPanel, BorderLayout.WEST); //add the list to the left of the frame
		
		log = new JTextArea(); //create the log
		log.setEditable(false); //so it can't be directly edited
		//log.setPreferredSize(new Dimension(340, 250));
		pane.add(new JScrollPane(log), BorderLayout.CENTER); //add to the right of the pane inside a scrolling pane
		
		setVisible(true); //make the frame visible
	}
	
	
	/**To add a connection to the list
	 * @param IPAddress the IP address of the host to add, eg, "183.92.32.122"
	 */
	public void addConnection(String IPAddress)
	{
		listModel.addElement(IPAddress);
	}

	/**To remove a connection to the list
	 * @param IPAddress the IP address of the host to remove, eg, "183.92.32.122"
	 * @return Whether there was a connection to remove
	 */
	public boolean removeConnection(String string)
	{
		boolean r = listModel.contains(string);
		listModel.removeElement(string);
		return r;
	}

	/**To write to the log
	 * @param string this is written on a new line in the log.
	 */
	public void writeLog(String string)
	{
		log.append(string + '\n');
		log.scrollRectToVisible(new Rectangle(0, log.getHeight() - 2, 1, 1));
	}
}
